package com.ymatou.iapi.socketio.testcase;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.Set;

import net.sf.json.JSONArray;

import org.apache.commons.lang3.tuple.Pair;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.pushbind.parameter.BindUserClientBean;
import com.ymatou.iapi.pushbind.parameter.UnBindUserClientBean;
import com.ymatou.iapi.pushbind.parameter.Enum.AppIdType;
import com.ymatou.iapi.pushbind.parameter.Enum.PhoneType;
import com.ymatou.iapi.pushbind.parameter.Enum.UserSystemType;
import com.ymatou.iapi.pushbind.service.UnBindUserClientCall;
import com.ymatou.iapi.socketio.socketio_stress;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.data.socket.DefaultSocketIo;
import com.ymttest.business.data.socket.SocketIoConnection.Listener;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.business.service.PushCallService;
import com.ymttest.database.redis.ImRedisWapper;
import com.ymttest.utils.YMTDateUtil;

public class Ts_socketio {

	private  UnBindUserClientBean unbinduserclientBean=new UnBindUserClientBean();
	private UnBindUserClientCall unbinduserclientCall=new UnBindUserClientCall();
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("socketio");
	}
	
	@Before
	public void caseUp() {
	}
	
	@AfterClass
	public static void tearDown() {
	
	}	
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_socketio_001() throws Exception {
		
		Logger.start(true,"socketio-登录时绑定(BindService->客服工作台)");
		Logger.comment("############前置条件 开始###################");
		UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
		UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
		Integer userId = urrc.getUserid();
		String clientId = String.valueOf((500000 + new Random().nextInt(1000000)));
		Logger.comment("############前置条件 开始###################");
		BindUserClientBean bindUserClientBean=PushCallService.intitUserClient(userId);
		bindUserClientBean.setAppId("s1.ymatou.com");
		bindUserClientBean.setUserSystemType(UserSystemType.CsUser);
		bindUserClientBean.setAppIdType(AppIdType.SocketIo);
		bindUserClientBean.setPhoneType(PhoneType.All);
		bindUserClientBean.setClientId(clientId);
		PushCallService.bindUserClient(bindUserClientBean);
		
		ImRedisWapper imRedis=new ImRedisWapper();
		Set<String> redis_str=imRedis.getBindUserRedis("BindRecord:UserBindSet:1:"+userId);
		net.sf.json.JSONObject jsonObject = JSONArray.fromObject(redis_str.toString() ).getJSONObject(0);
		System.out.println("redis_str:"+redis_str.toString());
		Logger.verifyEquals(bindUserClientBean.getUserId(), jsonObject.get("ui"), "验证redis中userid存在");
		Logger.verifyEquals(bindUserClientBean.getClientId(), jsonObject.get("ci"), "验证redis中clientId存在");
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_socketio_002() throws Exception {
		
		Logger.start(true,"socketio-下线时绑定(BindService->客服工作台)");
		Logger.comment("############前置条件 开始###################");
		UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
		UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
		Integer userId = urrc.getUserid();
		String clientId = String.valueOf((500000 + new Random().nextInt(1000000)));
		Logger.comment("############前置条件 开始###################");
		
		BindUserClientBean bindUserClientBean = PushCallService.intitUserClient(userId);
		bindUserClientBean.setAppId("s1.ymatou.com");
		bindUserClientBean.setUserSystemType(UserSystemType.CsUser);
		bindUserClientBean.setAppIdType(AppIdType.SocketIo);
		bindUserClientBean.setPhoneType(PhoneType.All);
		bindUserClientBean.setClientId(clientId);
		PushCallService.bindUserClient(bindUserClientBean);
		
		unbinduserclientBean.setAppId(bindUserClientBean.getAppId());
		unbinduserclientBean.setAppIdType(bindUserClientBean.getAppIdType());
		unbinduserclientBean.setClientId(bindUserClientBean.getClientId());
		unbinduserclientBean.setDeviceId(bindUserClientBean.getDeviceId());
		unbinduserclientBean.setDeviceToken("token" + YMTDateUtil.getDate());
		unbinduserclientBean.setUserId(bindUserClientBean.getUserId());
		unbinduserclientBean.setPhoneType(bindUserClientBean.getPhoneType());
		unbinduserclientBean.setUserSystemType(bindUserClientBean.getUserSystemType());
		unbinduserclientCall.setData(unbinduserclientBean);
		unbinduserclientCall.callService();		
		
		ImRedisWapper imRedis=new ImRedisWapper();
		Set<String> redis_str=imRedis.getBindUserRedis("BindRecord:UserBindSet:1:"+userId);
		System.out.println("redis_str:"+redis_str.toString());
		JSONArray jsonObject = JSONArray.fromObject(redis_str.toString() );
		Logger.verifyEquals(0, jsonObject.size(), "验证redis 解绑后数据为空");
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_socketio_003() throws Exception {
		
		socketio_stress s=new socketio_stress();
		Logger.start(true,"socketio-发送消息，接受到数据");
		Logger.comment("############前置条件 开始###################");
		UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
		UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
		Integer userId = urrc.getUserid();
		String clientId = String.valueOf((500000 + new Random().nextInt(1000000)));
		Logger.comment("############前置条件 开始###################");
		
		SimpleDateFormat df3 = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
		DefaultSocketIo socketIo=new DefaultSocketIo("http://172.16.101.73:8085");//soslave.ymatou.com	
		
		socketIo.addLinstner("receivemsg",new Listener() {
            public void call(Object... args) throws JSONException {
				if(args[0].toString().contains(df3.format(new Date()).toString()))
				{
					JSONObject jsonObject = new JSONObject(args[0].toString() );
					Logger.verifyNotNull(jsonObject.get("msg"), "验证接受的数据不为空");
					System.out.println("jsonObject:"+jsonObject);
					return;
				}
            }
        });//soslave接收消息
		
		socketIo.connect();
		//Thread.sleep(2000);
		socketIo.emit("guid", clientId);//soslave发送消息
		s.BindUserClient(userId,clientId);
		
		Integer cs_userId=s.sendMsg();//买家或者买手 发消息 给客服发消息
		String msg=s.SocketIoPush(cs_userId,userId);//socketIoPush 给 soslave 发送消息
		//System.in.read();
	}
}
